import requests 
import os
import json

from models.basemodel import BaseModel
from typing import List, Dict
class OllamaModel(BaseModel):
    platform_name = "ollama"

    def __init__(self):
        super().__init__()

        self.chat_url = "http://192.168.110.201:11434"
        self.model_name = "deepseek-chat"

        response = requests.get(f"{self.api_base}/api/tags")
        response.raise_for_status()
        available_models = [model["name"] for model in response.json().get("models", [])]

    def setModelName(self, model_name):
        self.model_name = model_name

    def chat(self, messages:List[Dict[str, str]]):
        data = {
                "model": self.model_name,
                "messages": messages,
                "stream": True  
            }
        response = requests.post(
                f"{self.api_base}/api/chat",
                json=data,
                stream=True
            )
        response.raise_for_status()
        for line in response.iter_lines():
            if line:
                chunk = line.decode()
                result = json.loads(chunk)
                if "message" in result and "content" in result["message"]:
                        yield result
    def name(self):
         return self.model_name
    
    def deleteChat(self):
         return True
    
    def upload_files(self, file_list):
        return []
    
    def getModelList(self):
        response = requests.get(f"{self.api_base}/api/tags")
        response.raise_for_status()
        return [(model["name"], "") for model in response.json().get("models", [])]